package com.agrm.cart.controller;

import com.agrm.cart.service.CartService;
import com.agrm.model.cart.dto.CartDeleteDto;
import com.agrm.model.cart.pojo.Cart;
import com.agrm.model.cart.pojo.CartGoods;
import com.agrm.model.common.dtos.PageRequestDto;
import com.agrm.model.common.dtos.ResponseResult;
import com.agrm.model.common.enums.AppHttpCodeEnum;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("api/v1/cart")
public class CartController {

    @Autowired
    private CartService cartService;


    /**
     * 获取用户全部购物车信息
     * @return
     */
    @GetMapping("/getAll")
    public ResponseResult getAll(Integer memberId) {
        List<Cart> lists = cartService.getAllCarts(memberId);
        return ResponseResult.okResult(lists);
    }


    /**
     * 根据传入的ids获取购物车内的商品信息
     * @param ids
     * @return 商品信息 数量 以及原价和总价
     */
    @PostMapping("/list")
    public ResponseResult list(@RequestBody List<Integer> ids){
        if(ids == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        CartGoods cartGoods = cartService.listGoodByCart(ids);
        return ResponseResult.okResult(cartGoods);
    }


    /**
     * 将商品添加到购物车 如果购物车中已经存在此商品 则数量加1
     * @param
     * @return
     */
    @PostMapping("/add")
    public ResponseResult addCart(@RequestBody Cart cart) {
        if(cart == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        return cartService.addCart(cart);
    }


    /**
     * 删除购物车
     * @param cartDelete
     * @return 删除的 数量
     */
    @DeleteMapping("/delete")
    public ResponseResult deleteCart(CartDeleteDto cartDelete) {
        if(cartDelete == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        int res = cartService.deleteCartByIds(List.of(cartDelete.getCartId()));
        return ResponseResult.okResult("共删除了" + res + "条购物车数据");
    }


    @DeleteMapping("deleteByIds")
    public ResponseResult deleteCartByIds(@RequestParam("cardIds") List<Integer> ids) {
        int res = cartService.deleteCartByIds(ids);
        return ResponseResult.okResult(res);
    }


}
